package newWork.controller;


import newWork.entity.*;
import newWork.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/router")
@Controller
public class RouterController {

    @Autowired
    private IClientService clientService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IStaffService staffService;

    @Autowired
    private IOptionService optionService;

    @Autowired
    private IStaffProjectService staffProjectService;

    /**
     * 调整到添加client页面 且携带client用户信息
     * @param clientId
     * @return
     */
    @GetMapping("/clientInfo/{clientId}")
    public String clientInfo(@PathVariable Integer clientId, Model model){
        ClientEntity clientEntity = new ClientEntity();
        clientEntity.setClientId(clientId);
        List<ClientEntity> client = clientService.select(clientEntity);
        model.addAttribute("client",client.get(0));
        return "/master/clientInfo";
    }

    @GetMapping("/updateProject/{projectId}")
    public String editProject(@PathVariable Integer projectId, Model model){
        ProjectEntity project = new ProjectEntity();
        project.setProjectId(projectId);
        List<ProjectEntity> projectInfo = projectService.select(project);
        model.addAttribute("project",projectInfo.get(0));
        return "/master/editProject";
    }

    @GetMapping("/updateClient/{clientId}")
    public String editClient(@PathVariable Integer clientId, Model model){
        ClientEntity clientEntity = new ClientEntity();
        clientEntity.setClientId(clientId);
        List<ClientEntity> select = clientService.select(clientEntity);
        model.addAttribute("client",select.get(0));
        return "/admin/editClient";
    }


    @GetMapping("/updateStaff/{staffId}")
    public String editStaffId(@PathVariable Integer staffId, Model model){
        StaffEntity staffEntity = new StaffEntity();
        staffEntity.setStaffId(staffId);
        List<StaffEntity> staffEntities = staffService.select(staffEntity);
        model.addAttribute("staff",staffEntities.get(0));
        return "/admin/editStaff";
    }

    /**
     * 计算工资 将信息放入model且跳转至相应的页面（iframe嵌套）
     * @param staffId
     * @param model
     * @return
     */
    @GetMapping("/calc/{staffId}")
    public String calc(@PathVariable Integer staffId, Model model){
        StaffProjectEntity staffProjectEntity = new StaffProjectEntity();
        staffProjectEntity.setStaffId(staffId);
        //查询到员工项目表中的信息
        List<StaffProjectEntity> select = staffProjectService.select(staffProjectEntity);
        model.addAttribute("salary",select);
        return "/admin/calcSalary";
    }

    /**
     * 项目的员工信息
     * @param projectId
     * @param model
     * @return
     */
    @GetMapping("/staffOfProject/{projectId}")
    public String staffOfProject(@PathVariable Integer projectId, Model model){
        ProjectEntity project = new ProjectEntity();
        project.setProjectId(projectId);
        List<ProjectEntity> projectInfo = projectService.select(project);
        //获取当前项目信息
        ProjectEntity projectEntity = projectInfo.get(0);
        //后期
        String[] lastHead = null,lastStaff = null;
        if (projectEntity.getLastHead()!=null) {
            lastHead = projectEntity.getLastHead().split(",");
        }
        if (projectEntity.getLastStaff()!=null) {
            lastStaff = projectEntity.getLastStaff().split(",");
        }
        //查询信息
        List<StaffEntity> Last = staffService.batchSelect(getIntegerList(lastHead, lastStaff));
        //模型主管
        String[] modelHead = null,modelStaff = null;
        if (projectEntity.getModelHead()!=null) {
            modelHead = projectEntity.getModelHead().split(",");
        }
        if (projectEntity.getModelStaff()!=null) {
            modelStaff = projectEntity.getModelStaff().split(",");
        }

        List<StaffEntity> Model = staffService.batchSelect(getIntegerList(modelHead, modelStaff));
        //渲染主管
        String[] renderHead = null,renderStaff = null;
        if (projectEntity.getRenderHead()!=null) {
            renderHead = projectEntity.getRenderHead().split(",");
        }
        if (projectEntity.getRenderStaff()!=null) {
            renderStaff = projectEntity.getRenderStaff().split(",");
        }

        List<StaffEntity> Render = staffService.batchSelect(getIntegerList(renderHead, renderStaff));

        model.addAttribute("last",Last);
        model.addAttribute("model",Model);
        model.addAttribute("render",Render);
        return "/master/staffOfProject";
    }

    @GetMapping("/option/{optionId}")
    public String optionInfo(@PathVariable("optionId")Integer optionId,Model model){
        OptionEntity optionEntity = new OptionEntity();
        optionEntity.setOptionId(optionId);
        List<OptionEntity> select = optionService.select(optionEntity);
        model.addAttribute("option",select.get(0));
        return "/master/editOption";
    }



    public List<Integer> getIntegerList(String []list1, String []list2){
        List<Integer> result = new ArrayList<>();
        if (list1==null&&list2==null){
            //防止mybatis查询报错
            result.add(-1);
        }
        if (list1!=null) {
            for (String string : list1) {
                result.add(Integer.parseInt(string));
            }
        }
        if (list2!=null) {
            for (String string : list2) {
                result.add(Integer.parseInt(string));
            }
        }
        return result;
    }

    /**
     *
     * 分配员工任务
     * @return
     */
    @GetMapping("/task/{projectId}")
    public String task(@PathVariable("projectId") Integer projectId,Model model){
        model.addAttribute("projectId",projectId);
        return "master/task";
    }
}
